import MySQLdb
import sys
import urllib
import re
import json

def get_good_annot(id_doc):
  url ='http://glejeune.perso.info.unicaen.fr/annotation/public/index.php?id='
  fichier = urllib.urlopen(url+str(id_doc)).read()
  patt_annot = re .compile("<div class='annotation'>(.*?)</div>")
  annotations = re.findall(patt_annot,fichier)
  liste = []
  patt_details = re.compile("<span class='annot_disease'>(.*?)</span> place : <span class='annot_place'>(.*?)</span> Number of cases : <span class='annot_cases'>(.*?)</span>")
  t = re.findall(patt_details,fichier)
  for a in annotations:
    test = re.search(patt_details,a)
    liste.append([test.group(1),test.group(2),test.group(3)])
  return liste
def creer_json_annot(docs,annots):
  for a in annots:
    print a
    for i in a:
      print i
    break
  for a in docs:
    print a
    break
  b/0

if __name__ =="__main__":
  password = sys.argv[1]
  db = MySQLdb.connect(host="mysql.info.unicaen.fr",user="glejeune",passwd=password,db="glejeune_8")
  c = db.cursor()
  c.execute("""SELECT * FROM documents""")
  liste_doc= c.fetchall()
  print len(liste_doc),'documents'
  c.execute("""SELECT * FROM annotations""")
  liste_annot = c.fetchall()
  print len(liste_annot),'annotations'
#  creer_json_annot(liste_doc,liste_annot)
  dic_sortie = {}
  for fic in liste_doc:
    id_doc,path,date,_,source,url,titre,langue,_,_ = fic
    date = str(date)
    infos_fic= {'path':path,'date_collecte':date,'langue':langue,'url':url,'annotations':[]}
    dic_sortie[id_doc]=infos_fic
  for annot in liste_annot:
    try:
      id_a,date,m,l,c,_,id_doc,_ = annot
    except:
      print annot,b/0
    triplet = [m,l,c]
    if m!='N':
      print m
      t = get_good_annot(id_doc)
      print t
    else:
      t=[triplet]
#    if t!=[]:
#      print t
    try:
      dic_sortie[id_doc]['annotations']=t
    except:
      pass
#  for id_doc in dic_sortie.keys():
#    dic_sortie[id_doc]=str(dic_sortie[id_doc])
  wfi = open('dico_ida.json','w')
  j = json.dumps(dic_sortie,sort_keys=True, indent=2)
  wfi.write("%s\n"%j)
  wfi.close()
  ofi = open('dico_ida.json','r').read()

  d = eval(ofi)














